<template>
  <view class="container">
    <view class="content">
      <!-- 标题区域 -->
      <view class="title-area">
        <text class="title">仓库编码验证</text>
        <text class="subtitle">请输入您的仓库编码以继续</text>
      </view>

      <!-- 输入区域 -->
      <view class="input-area">
        <view class="input-item">
          <text class="label">仓库编码</text>
          <input 
            class="store-code-input"
            type="text" 
            v-model="storeCode"
            placeholder="请输入仓库编码"
            placeholder-class="placeholder-style"
            :maxlength="20"
            @input="clearError"
          />
        </view>
        
        <text v-if="errorMessage" class="error-text">{{ errorMessage }}</text>
      </view>

      <!-- 按钮区域 -->
      <view class="button-area">
        <button 
          class="submit-btn"
          :class="{ 'disabled': !storeCode?.trim() || isValidating }"
          :disabled="!storeCode?.trim() || isValidating"
          @tap="validateStoreCode"
        >
          <text v-if="!isValidating">验证并继续</text>
          <text v-else>验证中...</text>
        </button>
      </view>

      <!-- 提示信息 -->
      <view class="tips-area">
        <text class="tips-title">提示：</text>
        <text class="tips-content">• 仓库编码由管理员提供
        \n• 编码区分大小写
        \n• 如有疑问请联系管理员</text>
      </view>
    </view>

    <!-- 加载遮罩 -->
    <view v-if="isValidating" class="loading-mask">
      <view class="loading-content">
        <view class="loading-spinner"></view>
        <text class="loading-text">正在验证仓库编码...</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import http from '../../utils/request'
import { useUserStore } from '../../store/user'
  import { onLoad } from '@dcloudio/uni-app'
  import { getDeviceId } from '../../utils/device'


  // 初始化用户store
  const userStore = useUserStore()

// 响应式数据
const storeCode = ref('')
const errorMessage = ref('')
const isValidating = ref(false)

// 仓库编码验证方法
const validateStoreCode = async () => {
  // 清除之前的错误信息
  errorMessage.value = ''
  
  // 基础验证
  if (!storeCode.value.trim()) {
    errorMessage.value = '请输入仓库编码'
    return
  }
  
  
  // 设置验证状态
  isValidating.value = true
  
  try {
    // 模拟API验证（实际项目中替换为真实API调用）
    await simulateApiValidation(storeCode.value.trim())
    
    // 验证成功，保存仓库编码到本地存储
  //  uni.setStorageSync('store_code', storeCode.value.trim())
  //  uni.setStorageSync('store_validated', true)
    
    // 跳转到登录页面
    uni.redirectTo({
      url: '/pages/login/index'
    })
    
  } catch (error) {
    console.log('error', error);
    
    errorMessage.value = error.message || '仓库编码验证失败'
  } finally {
    isValidating.value = false
  }
}

// 清除错误信息
const clearError = () => {
  errorMessage.value = ''
}

// 模拟API验证（实际项目中替换为真实API调用）
const simulateApiValidation = async (code) => {
  console.log('code', code)
  try {
    const res = await http.post('https://www.dachen.vip/api/workflow/hooks/NjhjMmI3M2JjZTdlMjFhZDQ2NzFhNTE0', {
      storeCode: code
    })
    console.log('验证结果:', res)
    // 检查响应数据
    if (res) {
      return res.data
    } else {
      throw new Error(res.error || '仓库编码无效')
    }
  } catch (error) {
    console.error('验证请求失败:', error)
    throw error
  }
}

// 页面加载时检查是否已验证
const checkValidationStatus = () => {
  const isValidated = uni.getStorageSync('userInfo')
  if (isValidated) {
      userStore.setUserInfo(JSON.parse(isValidated))

    console.log('userStore', userStore.getUserInfo());

    uni.redirectTo({
      url: '/pages/tabs/index'
    })
  }
}

// 页面加载时执行
onLoad(async () => {
  checkValidationStatus()
})
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
}

.content {
  width: 100%;
  max-width: 600rpx;
  background: #ffffff;
  border-radius: 24rpx;
  padding: 80rpx 60rpx;
  box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.1);
}

.title-area {
  text-align: center;
  margin-bottom: 80rpx;
}

.title {
  display: block;
  font-size: 48rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 16rpx;
}

.subtitle {
  display: block;
  font-size: 28rpx;
  color: #666666;
}

.input-area {
  margin-bottom: 60rpx;
}

.input-item {
  margin-bottom: 20rpx;
}

.label {
  display: block;
  font-size: 32rpx;
  font-weight: 500;
  color: #333333;
  margin-bottom: 20rpx;
}

.store-code-input {
  width: 90%;
  height: 100rpx;
  background: #f8f9fa;
  border: 2rpx solid #e9ecef;
  border-radius: 16rpx;
  padding: 0 30rpx;
  font-size: 32rpx;
  color: #333333;
  transition: all 0.3s ease;
}

.store-code-input:focus {
  border-color: #667eea;
  background: #ffffff;
  box-shadow: 0 0 0 4rpx rgba(102, 126, 234, 0.1);
}

.placeholder-style {
  color: #adb5bd;
  font-size: 32rpx;
}

.error-text {
  display: block;
  color: #dc3545;
  font-size: 28rpx;
  margin-top: 12rpx;
  margin-left: 10rpx;
}

.button-area {
  margin-bottom: 60rpx;
}

.submit-btn {
  width: 100%;
  height: 100rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 50rpx;
  color: #ffffff;
  font-size: 32rpx;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  box-shadow: 0 8rpx 16rpx rgba(102, 126, 234, 0.3);
}

.submit-btn:active {
  transform: translateY(2rpx);
  box-shadow: 0 4rpx 8rpx rgba(102, 126, 234, 0.3);
}

.submit-btn.disabled {
  background: #e9ecef;
  color: #adb5bd;
  box-shadow: none;
}

.tips-area {
  background: #f8f9fa;
  border-radius: 16rpx;
  padding: 40rpx;
}

.tips-title {
  display: block;
  font-size: 28rpx;
  font-weight: bold;
  color: #495057;
  margin-bottom: 20rpx;
}

.tips-content {
  display: block;
  font-size: 26rpx;
  color: #6c757d;
  line-height: 1.6;
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-content {
  background: #ffffff;
  border-radius: 24rpx;
  padding: 60rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid #f3f3f3;
  border-top: 4rpx solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 30rpx;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 28rpx;
  color: #666666;
}
</style>